import request from "@/common/utils/request";
import store from "@/store";

const globalData = {
  state: {
    isMobile: false, //是否是手机访问
    isIframe: false, //是否是iframe访问
    captcha: {captcha_type: 0}, // 登录验证 captcha_type:0不开启，1开启腾讯云
    basicConfig: {}, // 系统基础配置
    returnConfig: {}, // 后端返回的配置项

    // 当前选中的子菜单
    loadedSubMenu: {},
    loginPic: "", // 登录左侧图

    microSendMessage: {}, // 从子应用或iframe中传来的，需要打开的微应用信息
    customSideMenu: '', // 定制menu
    customCompany: '', // 定制公司
    showMaxContent: false,
    refreshMicroApp: 0,
  },
  mutations: {
    IS_MOBILE: (state, mobile) => {
      state.isMobile = mobile ? true : false;
    },
    IS_IFRAME: (state, iframe) => {
      state.isIframe = iframe ? true : false;
    },
    ORG_CONFIG: (state, data) => {
      state.basicConfig = {...data.basic, ...data.contact};
      state.returnConfig = {...data};
      state.captcha = data.captcha || state.captcha;
      state.loginPic = data?.org?.login?.org_login_pic || "";
      const mapKey = data.map.tiandutu_map_key;
      if (mapKey) {
        let script = document.createElement("script");
        script.setAttribute('ignore', 'true');
        script.type = "text/javascript";
        script.src = `https://api.tianditu.gov.cn/api?v=4.0&tk=${mapKey}`;
        document.getElementsByTagName("head")[0].appendChild(script);
      }
      const {system_square_logo, system_logo} = state.basicConfig;
      let iconLink = system_square_logo || system_logo;
      if (iconLink) {
        let $favicon = document.querySelector('link[rel="icon"]');
        if ($favicon !== null) {
          $favicon.href = iconLink;
        } else {
          $favicon = document.createElement("link");
          $favicon.rel = "icon";
          $favicon.href = iconLink;
          document.head.appendChild($favicon);
        }
      }
    },
    SET_LOADED_SUB_MENU: (state, {topAlias, subAlias}) => {
      state.loadedSubMenu[topAlias] = subAlias;
    },
    SET_MICRO_SEND_MESSAGE: (state, data) => {
      state.microSendMessage = data;
    },
    SET_CUSTOM_SIDE_MENU: (state, data) => {
      state.customSideMenu = data;
    },
    SET_CUSTOM_COMPANY: (state, data) => {
      state.customCompany = data;
    },
    SET_SHOW_MAX_CONTENT: (state, data) => {
      state.showMaxContent = !!data;
    },
    SET_REFRESH_MICRO_APP: (state, data) => {
      state.refreshMicroApp = data;
    },
  },
  actions: {
    // 手机访问
    setIsMobile({commit}, mobile) {
      commit("IS_MOBILE", mobile);
    },
    //iframe访问
    setIsIframe({commit}, iframe) {
      commit("IS_IFRAME", iframe);
    },
    // 存储系统配置信息
    setSystemConfig({commit}, data) {
      commit("ORG_CONFIG", data);
    },
    setCustomSideMenu({commit}, data) {
      commit("SET_CUSTOM_SIDE_MENU", data);
    },
    // 每个菜单选中的对应的子菜单
    SetLoadedSubMenu: ({commit}, {topAlias, subAlias}) => {
      commit("SET_LOADED_SUB_MENU", {topAlias, subAlias});
    },
    // 从子应用或iframe中传来的，需要打开的微应用信息
    setMicroSendMessage({commit}, data) {
      return new Promise((resolve) => {
        commit("SET_MICRO_SEND_MESSAGE", data);
        resolve();
      });
    },
    setShowMaxContent({commit}, data) {
      commit("SET_SHOW_MAX_CONTENT", data);
    },
    setRefreshMicroApp({commit}, data) {
      commit("SET_REFRESH_MICRO_APP", data);
    },
    getConfig({commit, getters}) {
      return new Promise((resolve, reject) => {
        request
          .get("/public", "/system/config", {visitPort: "org"})
          .then((res) => {
            commit("ORG_CONFIG", res);
            commit("SET_CUSTOM_SIDE_MENU", res?.basic?.org_extra_config?.sso_login === 'xiaomeisha');
            commit("SET_CUSTOM_COMPANY", res?.basic?.org_extra_config?.sso_login);
            store.dispatch("SetLang", res.basic.system_lang);
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    setConfig({commit}, res) {
      commit("ORG_CONFIG", res);
      commit("SET_CUSTOM_SIDE_MENU", res?.basic?.org_extra_config?.sso_login === 'xiaomeisha');
      commit("SET_CUSTOM_COMPANY", res?.basic?.org_extra_config?.sso_login);
      store.dispatch("SetLang", res.basic.system_lang);
    },
  },
};
window.mapInit = () => {
  // console.log("window.mapInit", qq.maps);
};
export default globalData;
